{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "87ecc9f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: openai==1.82.0 in e:\\python310\\lib\\site-packages (1.82.0)\n",
      "Requirement already satisfied: typing-extensions<5,>=4.11 in c:\\users\\t\\appdata\\roaming\\python\\python310\\site-packages (from openai==1.82.0) (4.13.2)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in e:\\python310\\lib\\site-packages (from openai==1.82.0) (0.28.1)\n",
      "Requirement already satisfied: jiter<1,>=0.4.0 in e:\\python310\\lib\\site-packages (from openai==1.82.0) (0.10.0)\n",
      "Requirement already satisfied: tqdm>4 in e:\\python310\\lib\\site-packages (from openai==1.82.0) (4.67.1)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in e:\\python310\\lib\\site-packages (from openai==1.82.0) (1.9.0)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in e:\\python310\\lib\\site-packages (from openai==1.82.0) (4.9.0)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in e:\\python310\\lib\\site-packages (from openai==1.82.0) (2.11.7)\n",
      "Requirement already satisfied: sniffio in e:\\python310\\lib\\site-packages (from openai==1.82.0) (1.3.1)\n",
      "Requirement already satisfied: idna>=2.8 in e:\\python310\\lib\\site-packages (from anyio<5,>=3.5.0->openai==1.82.0) (3.10)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in c:\\users\\t\\appdata\\roaming\\python\\python310\\site-packages (from anyio<5,>=3.5.0->openai==1.82.0) (1.3.0)\n",
      "Requirement already satisfied: httpcore==1.* in e:\\python310\\lib\\site-packages (from httpx<1,>=0.23.0->openai==1.82.0) (1.0.9)\n",
      "Requirement already satisfied: certifi in e:\\python310\\lib\\site-packages (from httpx<1,>=0.23.0->openai==1.82.0) (2025.4.26)\n",
      "Requirement already satisfied: h11>=0.16 in e:\\python310\\lib\\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai==1.82.0) (0.16.0)\n",
      "Requirement already satisfied: typing-inspection>=0.4.0 in e:\\python310\\lib\\site-packages (from pydantic<3,>=1.9.0->openai==1.82.0) (0.4.1)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in e:\\python310\\lib\\site-packages (from pydantic<3,>=1.9.0->openai==1.82.0) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.33.2 in e:\\python310\\lib\\site-packages (from pydantic<3,>=1.9.0->openai==1.82.0) (2.33.2)\n",
      "Requirement already satisfied: colorama in c:\\users\\t\\appdata\\roaming\\python\\python310\\site-packages (from tqdm>4->openai==1.82.0) (0.4.6)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "[notice] A new release of pip is available: 23.0.1 -> 25.1.1\n",
      "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
     ]
    }
   ],
   "source": [
    "!pip install openai==1.82.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "09bac09c",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "请设置 DEEPSEEK_API_KEY 环境变量",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[4], line 7\u001b[0m\n\u001b[0;32m      5\u001b[0m api_key \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mgetenv(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDEEPSEEK_API_KEY\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m      6\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m api_key:\n\u001b[1;32m----> 7\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m请设置 DEEPSEEK_API_KEY 环境变量\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m      9\u001b[0m \u001b[38;5;66;03m# 初始化 OpenAI 客户端（假设 DeepSeek 的 API 兼容 OpenAI 格式）\u001b[39;00m\n\u001b[0;32m     10\u001b[0m client \u001b[38;5;241m=\u001b[39m OpenAI(\n\u001b[0;32m     11\u001b[0m     api_key\u001b[38;5;241m=\u001b[39mapi_key,\n\u001b[0;32m     12\u001b[0m     base_url\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhttps://api.deepseek.com/v1\u001b[39m\u001b[38;5;124m\"\u001b[39m,  \u001b[38;5;66;03m# DeepSeek API 的基地址\u001b[39;00m\n\u001b[0;32m     13\u001b[0m )\n",
      "\u001b[1;31mValueError\u001b[0m: 请设置 DEEPSEEK_API_KEY 环境变量"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from openai import OpenAI\n",
    "\n",
    "# 从环境变量获取 DeepSeek API Key\n",
    "api_key = os.getenv(\"DEEPSEEK_API_KEY\")\n",
    "if not api_key:\n",
    "    raise ValueError(\"请设置 DEEPSEEK_API_KEY 环境变量\")\n",
    "\n",
    "# 初始化 OpenAI 客户端（假设 DeepSeek 的 API 兼容 OpenAI 格式）\n",
    "client = OpenAI(\n",
    "    api_key=api_key,\n",
    "    base_url=\"https://api.deepseek.com/v1\",  # DeepSeek API 的基地址\n",
    ")\n",
    "\n",
    "# 定义提示词\n",
    "prompt = \"\"\"请帮我用 HTML 生成一个五子棋游戏，所有代码都保存在一个 HTML 中。\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0b31c61b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "五子棋游戏已保存为 gomoku.html\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    # 调用 DeepSeek Chat API\n",
    "    response = client.chat.completions.create(\n",
    "        model=\"deepseek-chat\",  # 或 DeepSeek 提供的其他模型名称\n",
    "        messages=[\n",
    "            {\"role\": \"system\", \"content\": \"你是一个专业的 Web 开发助手，擅长用 HTML/CSS/JavaScript 编写游戏。\"},\n",
    "            {\"role\": \"user\", \"content\": prompt}\n",
    "        ],\n",
    "        temperature=0.7,\n",
    "        stream=False\n",
    "    )\n",
    "\n",
    "    # 提取生成的 HTML 内容\n",
    "    if response.choices and len(response.choices) > 0:\n",
    "        html_content = response.choices[0].message.content\n",
    "        \n",
    "        # 保存到文件\n",
    "        with open(\"gomoku.html\", \"w\", encoding=\"utf-8\") as f:\n",
    "            f.write(html_content)\n",
    "        print(\"五子棋游戏已保存为 gomoku.html\")\n",
    "    else:\n",
    "        print(\"未收到有效响应\")\n",
    "\n",
    "except Exception as e:\n",
    "    print(f\"调用 API 出错: {e}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
